package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import sun.security.krb5.internal.PAEncTSEnc;

import com.ejuornal.bean.Artical;
import com.ejuornal.bean.Page;
import com.ejuornal.bean.Store;
import com.ejuornal.dao.StoreDao;

@Component("storeDao")
public class StoreDaoImpl extends DaoSupport<Store> implements StoreDao {

	public Store findById(int storeId) {
		String sql = "SELECT * FROM t_store WHERE storeId = ?";
		Store store = super.getJdbcTemplate().queryForObject(sql,
				new Object[] { storeId }, new RowMapper<Store>() {
					public Store mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Store store = new Store();
						store.setStoreId(rs.getInt("storeId"));
						store.setUserId(rs.getInt("userId"));
						store.setContentId(rs.getInt("contentId"));
						store.setType(rs.getString("type"));
						store.setCollectDate(rs.getDate("collectDate"));

						return store;
					}
				});

		return store;
	}

	//未写完
	public Page<Store> findByProperty(int current_record, String url, String prop, Object value) {
		String sql = "SELECT * FROM t_store WHERE ? = ?";
		Page<Store> page = new Page<Store>();
		
		List<Store> storeList = super.getJdbcTemplate().query(sql,
				new Object[] { prop, value }, new RowMapper<Store>() {
					public Store mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Store store = new Store();
						store.setStoreId(rs.getInt(""));
						
						return store;
					}
				});
		
		page.setRecords(storeList);

		return page;
	}
	
	public void add(String sql, Object... objs) {
		super.add(sql, objs);
	}
	
	public void batchDelete(String sql ,final int...ids) {
		super.batchDelete(sql, ids);
	}
	
	@Override
	public Page<Store> find_2(int current_record, String url,
			Object[] objs) { 
		Page<Store> page = new Page<Store>();
		String sql_1 ="SELECT count(*) FROM t_store s WHERE s.userId = ?";
		String sql_2 = "SELECT s.storeId,s.contentId,a.title,s.collectDate FROM t_store s," +
						"t_artical a WHERE  s.contentId= a.articalId and s.userId = ?" +
						" limit " + current_record + "," + 10;
		int totalRecords = this.getJdbcTemplate().queryForInt(sql_1, objs);
		List list = this.getJdbcTemplate().query(sql_2, objs, 
							new RowMapper() {
								public Object mapRow(ResultSet rs, int arg1)
										throws SQLException {
									Store store = new Store();
									store.setStoreId(rs.getInt("storeId"));
									store.setContentId(rs.getInt("contentId"));
									store.setCollectDate(rs.getDate("collectDate"));
									Artical artical = new Artical();
			                        artical.setTitle(rs.getString("title"));
			                        store.setArticle(artical);
			                        return store;
								}
			
		});
		page.setUrl(url);
        page.setTotalRecords(totalRecords);
        page.setTotalPage(totalRecords);
        page.setRecords(list);
        return page;
	}

	@Override
	public int checkIsStore(int userId, int articalId) {
		String sql = "select count(*) from t_store where userId = ? and contentId = ?";
		int  count = super.getJdbcTemplate().queryForInt(sql, new Object[]{userId,articalId});
		return count;
	}

	
}
